<?php
namespace Home\Model;
use Think\Model;
use Common\Common\Classes\Message;

class ClassService extends Model{
    private $cla_sql;
    public function __construct(){
        $this->cla_sql = D("Class");
    }
    //---------------------------------新增班级---------------------------------
    public function addClass(){
        $checkCode['cla_code'] = $this->getCode();
        while(!$this->cla_sql->create($checkCode,4)){
            $checkCode['cla_code'] = $this->getCode();
        }
        $checkName["cla_name"] = $_POST["cla_name"];
        $checkName["cou_id"] = $_POST["cou_id"];
        if($this->checkName($checkName)) {
            $_POST["cla_code"] = $checkCode['cla_code'];
            if ($this->cla_sql->create($_POST, 1)) {
                $this->cla_sql->add();
                $data = new Message(1, "新增班级成功!", "");
            } else {
                $data = new Message(0, $this->cla_sql->getError(), "");
            }
        }
        else{
            $data = new Message(0, "该班级已经存在", "");
        }
        return $data;
    }
    //---------------------------------生成一个班级代码---------------------------------
    public function getCode(){
        $time = uniqid();
        return strtoupper(substr($time, 5, 12));
    }
    //---------------------------------检测是否有同名班级---------------------------------
    private function checkName($where){
        if($this->cla_sql->where($where)->find()){
            return false;
        }
        return true;
    }
    //---------------------------------自动将过期班级归档---------------------------------
    public function filedClass(){
        $where["is_filed"] = 0;
        $where["cla_end"] = array('lt',date('Y-m-d'));
        $update["is_filed"] = 1;
        $this->cla_sql->where($where)->save($update);
    }
    //---------------------------------获得老师的未归档所有班级---------------------------------
    public function getAllClasses($type,$is_filed){
        $where["teacher_tea_id"] = $_SESSION["id"];
        if($type == 1) {
            $where["is_filed"] = $is_filed;         //$is_filed就是归不归档的标志,0是未归档,1是归档的意思
        }
        else if($type == 0){         //班级导入专用
            $where["cla_id"] = array("not in",$_GET["cad"]);
        }
        $field = array("cla_id", "cla_name", "cla_stu_num", "cla_start", "cla_end","cou_name","cou_id");
        $result = D("classView")->where($where)->order("cla_start desc")->field($field)->select();
        return $result;
    }
    //---------------------------------获得指定课程的所有班级---------------------------------
    public function getClassByCourse($is_filed) {
        $where["teacher_tea_id"] = $_SESSION["id"];
        $where["is_filed"] = $is_filed;          //$is_filed就是归不归档的标志,0是未归档,1是归档的意思
        $where["cou_id"] = $_REQUEST["cd"];
        $field = array("cla_id", "cla_name", "cla_stu_num", "cla_start", "cla_end","cou_name","cou_id");
        return D("classView")->where($where)->order("cla_start desc")->field($field)->select();
    }
    //---------------------------------删除某个班级---------------------------------
    public function deleteClass(){
        $cla_id = $_POST["cla_id"];
        $result = $this->cla_sql->delete($cla_id);
        if($result!=""){
            $data = new Message(1,"班级删除成功!","");
        }
        else{
            $data = new Message(0,"班级删除失败!","");
        }
        return $data;
    }
    //---------------------------------修改某个班级---------------------------------
    public function updateClass(){
        if($this->cla_sql->create($_POST,2)){       //自动验证一下提交过来的信息有没有问题
            $where["cla_id"] = $_POST["cla_id"];
            $update = $_POST;
            unset($update["cla_id"]);       //去除无用数据
            $result = $this->cla_sql->where($where)->save($update);     //修改
            if($result>0){
                $data = new Message(1,"班级修改生效!","");
            }
            else{
                $data = new Message(0,"班级修改失败!","");            //一般是班级信息没有任何改变才会失败
            }
        }
        else{
            $data = new Message(0,$this->cla_sql->getError(),"");       //验证不通过
        }
        return $data;
    }
    //---------------------------------检测班级是否可以删除---------------------------------
    public function checkIsDelete(){
        $where["cla_id"] = $_POST["cla_id"];
        $quiz = $this->cla_sql->relation("question_set")->where($where)->find();        //查处该班级下的试卷
        $quiz = $quiz["quiz"];
        $stu_num = $this->cla_sql->where($where)->field(array("cla_stu_num"))->find();
        $stu_num = $stu_num["cla_stu_num"];
        if(count($quiz) == 0 && $stu_num == 0){
            $data = new Message(1,"该班级可以删除!","");
        }
        else{
            $data = new Message(0,"该班级不能直接删除,该班级拥有学生或试卷!建议归档","");
        }
        return $data;
    }
    //---------------------------------班级手动归档---------------------------------
    public function fileClass(){
        $where["cla_id"] = $_POST["cla_id"];;
        $update["is_filed"] = 1;
        $result = $this->cla_sql->where($where)->save($update);
        if($result>0){
            $data = new Message(1,"班级归档成功!","");
        }
        else{
            $data = new Message(0,"班级归档失败!","");
        }
        return $data;
    }
    //---------------------------------班级重新启用---------------------------------
    public function renewClass(){
        $where["cla_id"] = $_POST["cla_id"];
        $where["cla_end"] = array("gt",date('Y-m-d'));
        $update["is_filed"] = 0;
        $result = $this->cla_sql->where($where)->save($update);
        if($result>0){
            $data = new Message(1,"班级恢复成功!","");
        }
        else{
            $data = new Message(0,"班级结束时间已过,无法恢复!","");
        }
        return $data;
    }
    //---------------------------------查看是否用权限查看某个班级的信息---------------------------------
    public function checkBelong(){
        $where["cla_id"] = $_GET["cad"];
        $where["cou_id"] = $_GET["cd"];
        $where["teacher_tea_id"] = $_SESSION["id"];
        if($this->cla_sql->relation("course")->where($where)->find()!=""){
            $data = new Message(1,"你拥有权限查看该班级信息!","");
        }
        else{
            $data = new Message(0,"你没有权限查看该班级信息!","");
        }
        return $data;
    }
    //---------------------------------获得某个班级的基本信息---------------------------------
    public function classInfo(){
        $where["cla_id"] = $_GET["cad"];
        $field = array("cla_id","cla_name","cla_code","is_auto","cla_stu_num","cou_id","cou_name");
        $result = D("ClassView")->where($where)->field($field)->find();
        return $result;
    }
    //---------------------------------获得其他班级的基本信息(除了已选班级的)---------------------------------
    public function getOtherClassInfo(){
        $where["cou_id"] = $_POST["cou_id"];
        $where["cla_id"] = array("not in",$_POST["cla_id"]);
        $field = array("cla_id","cla_name");
        $otherClass = $this->cla_sql->where($where)->field($field)->select();
        $data = new Message(1,"班级查询成功!",$otherClass);
        return $data;
    }
    //----------------------------------班级学生分页的基本信息(通过班级人数计算)------------------------------------
    public function getPageInfo(){
        $count = $this->cla_sql->field(array("cla_stu_num"))->find($_GET["cad"]);
        $pageInfo["count"] =  $count["cla_stu_num"]/1;
        $pageInfo["page"] = ceil($pageInfo["count"]/20);
        if($_REQUEST["np"]<=0 || $_REQUEST["np"] > $pageInfo["page"]){
            $pageInfo["np"] = 1;
        }
        else{
            $pageInfo["np"] = $_REQUEST["np"];
        }
        $pageInfo["np"] = $pageInfo["np"]/1;
        return $pageInfo;
    }
    //----------------------------------修改班级的人数----------------------------------------
    public function updateNum($type,$num){
        $where["cla_id"] = $_POST["class_cla_id"];
        if($type == 1){                         //1表示增加
            $this->cla_sql->where($where)->setInc("cla_stu_num",$num);
        }
        else if($type == 0){                    //0表示减少
            $this->cla_sql->where($where)->setDec("cla_stu_num",$num);
        }
        $num = $this->cla_sql->where($where)->field(array("cla_stu_num"))->find();
        return $num["cla_stu_num"];
    }
    //----------------------------------检测是否有权限对班级进行操作----------------------------------------
    public function  checkPower(){
        $where["teacher_tea_id"] = $_SESSION["id"];
        if($_GET["cad"]!="") {
            $where["cla_id"] = $_GET["cad"];
        }
        else if($_POST["cla_id"] != ""){
            $where["cla_id"] = $_POST["cla_id"];
        }
        $result = D("CheckClassView")->where($where)->find();
        if($result != ""){
            $data = new Message(1,"你有权限对该班级进行操作!","");
        }
        else{
            $data = new Message(0,"你没有权限对该班级进行操作!","");
        }
        return $data;
    }
    public function __destruct(){}
}